#!/bin/bash

export WORKSPACE="$(pwd)"
export PACKAGES_PATH=$WORKSPACE/edk2-libc:$WORKSPACE/edk2
export EDK_TOOLS_PATH=$WORKSPACE/edk2/BaseTools
export EDK2_LIBC_PATH=$WORKSPACE/edk2-libc

update_submodules() {
    git submodule update --init --recursive
    if [ $? -ne 0 ]; then
        echo -e "\\e[31m""Error: Failed to update git submodules""\\e[0m"
        return 1
    fi
    return 0
}

build_base_tools() {
    make -j"$(nproc)" -C $EDK_TOOLS_PATH
    if [ $? -ne 0 ]; then
        echo -e "\\e[31m""Error: Failed to build EDK2 BaseTools""\\e[0m"
        return 1
    fi
    return 0
}

setup_environment() {
    source $WORKSPACE/edk2/edksetup.sh
    if [ $? -ne 0 ]; then
        echo -e "\\e[31m""Error: Failed to source edksetup.sh""\\e[0m"
        return 1
    fi
    return 0
}

update_submodules || return 1
build_base_tools  || return 1
setup_environment || return 1
